package com.top10.controller;


import com.top10.bean.JwtUtil;
import com.top10.bean.ResponseBean;
import com.top10.common.DatabaseConst;
import com.top10.common.WebConst;
import com.top10.entity.Consignee;
import com.top10.entity.TbUserAddress;
import com.top10.service.impl.ConsigneeServiceImpl;
import com.top10.service.impl.TbUserAddressServiceImpl;
import com.top10.utils.GetUuid;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 收货地址Conroller
 */
@RestController
@RequestMapping("/manager/consignee")
public class ConsigneeController {
	@Autowired
	private ConsigneeServiceImpl consigneeService;
	@Autowired
	private TbUserAddressServiceImpl tbUserAddressService;
	@Autowired
	@Lazy
	private JwtUtil jwtUtil;

	/**
	 * 获取收货地址列表
	 * @param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getall",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean getAll(HttpServletRequest request)throws Exception{
		String token = request.getHeader(WebConst.TOKEN);
        String temp = jwtUtil.parseJwtPayload(token);
        JSONObject obj = JSONObject.fromObject(temp);
        String user_uuid=obj.getString("sub");

		List<Consignee> consignee=consigneeService.getAll(user_uuid);
		if(consignee.size() !=0){
			return ResponseBean.createSuccess().addData("consignee",consignee);
		}else {
			return ResponseBean.createError("你还没有收货地址,快去编辑");
		}
	}
	/**
	 * 编辑收货地址
	 */
	@RequestMapping(value = "/update",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean updateConsignee(Consignee consignee)throws Exception{
		if(consignee.getId() == 0){
			return ResponseBean.createError("参数为空");
		}
		consignee.setMender(consignee.getUser_uuid());
		consignee.setUpdatetime(new Date());
		Integer rest=consigneeService.updateConsignee(consignee);
		if(rest!=0){
			return ResponseBean.createSuccess().setMessage("编辑成功");
		}else{
			return ResponseBean.createError("编辑失败");
		}

	}
	/**
	 * 删除单条收货地址
	 */
	@RequestMapping(value = "/deleted",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean deletedConsignee(Integer id)throws Exception{
		if(id ==0){
			return  ResponseBean.createError("你输入的参数为空");
		}
		Integer rest=consigneeService.deletedConsignee(id);
		if(rest!=0){
			return ResponseBean.createSuccess().setMessage("删除成功");
		}else{
			return ResponseBean.createError("删除失败");
		}
	}
	/**
	 * 全部删除收货地址
	 */
	@RequestMapping(value = "/deletedall",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean deletedAll(@RequestParam(value="asd",required = false)String asd)throws Exception{
		String[] temp = asd.split(",");//以,拆分字符串
		int[] list = new int[temp.length];//int类型数组
		for(int i=0;i<temp.length;i++)
		{
			list[i] = Integer.parseInt(temp[i]);//整数数组
		}
		int a ;
		int sum = 0;
		int i = 0;
		if (list == null || (list != null && list.length == 0)) {
			return ResponseBean.createError("参数不为空");
		}
		for (Integer id : list) {
			try {
				a=consigneeService.deletedConsignee(id);
				sum = sum + a;
			} catch (Exception e) {
				i++;
			}
		}
		return ResponseBean.createSuccess().setMessage("清空"+sum+"记录");
	}
	/**
	 * 设置默认地址
	 */
	@RequestMapping(value = "/default",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean defaultConsignee(Integer id,HttpServletRequest request)throws Exception{
		if(id ==0){
			return  ResponseBean.createError("你输入的参数为空");
		}
		/*String userUuid="719656db8ea04b359b672bf24b6dfe9a";*/
		String userUuid = JwtUtil.getUuidByToken(request);
		Consignee consignee=consigneeService.selectConsignee(userUuid);
		if(consignee != null){
			consignee.setIsdefault(DatabaseConst.IS_DEFAULT_NO);
			consigneeService.updateConsignee(consignee);
		}
		Integer rest=consigneeService.defaultConsignee(id);
		if(rest!=0){
			return ResponseBean.createSuccess().setMessage("设置成功");
		}else{
			return ResponseBean.createError("设置失败");
		}
	}
	/**
	 * 增加收货地址
	 */
//	@RequestMapping(value = "/add",method = RequestMethod.POST)
//	@ResponseBody
//	public ResponseBean addConsignee(Consignee consignee,HttpServletRequest request)throws Exception{
//		String token = request.getHeader(WebConst.TOKEN);
//		String temp = jwtUtil.parseJwtPayload(token);
//		JSONObject obj = JSONObject.fromObject(temp);
//		String user_uuid=obj.getString("sub");
//
//		consignee.setUser_uuid(user_uuid);
//		consignee.setUuid(new GetUuid().newId());
//		consignee.setCreator(user_uuid);
//		consignee.setCreatetime(new Date());
//		Consignee consignee1=consigneeService.selectConsignee(user_uuid);//这是默认的地址
//		if(consignee1 ==null){
//			consignee.setIsdefault(true);
//		}
//
//		consignee.setIsdeleted(false);
//
//		Integer rest=consigneeService.addConsignee(consignee);
//		if(rest!=0){
//			return ResponseBean.createSuccess().setMessage("增加成功");
//		}else{
//			return ResponseBean.createError("增加失败");
//		}
//	}


	@RequestMapping(value = "/add",method = RequestMethod.POST)
	@ResponseBody
	public ResponseBean addConsignee(Consignee consignee,HttpServletRequest request)throws Exception{

		String userUuid = JwtUtil.getUuidByToken(request);
		//	String  userUuid="95153988018047CFA4D466F799FA9C4E";
		consignee.setUser_uuid(userUuid);
		consignee.setUuid(new GetUuid().newId());
		consignee.setCreator(userUuid);
		consignee.setCreatetime(new Date());

		Consignee consignee1=consigneeService.selectConsignee(userUuid);//这是默认的地址
		if(consignee1 ==null){
			consignee.setIsdefault(true);

		}
		if(consignee1!=null){
			consignee.setIsdefault(false);
		}

		consignee.setIsdeleted(false);



		Integer rest=consigneeService.addConsignee(consignee);
		if(rest!=0){
			return ResponseBean.createSuccess().setMessage("增加成功");
		}else{
			return ResponseBean.createError("增加失败");
		}
	}





}
